package com.seo.controller.keyWord;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.sdk.KeywordRevisionService;
import com.seo.service.user.sdk.ConfigService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: KeyWordController
 * @author rtc
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/keyWord/revision/")
public class KeyWordRevisionController {

	@Resource(name = "keywordRevisionService")
	private KeywordRevisionService keywordRevisionService;
	
	@Autowired
	private ConfigService configService;
	
	@Autowired
	private PageTool pageTool;
	
	/**
	 * 获取关键词指数
	 * 
	 * @param keyWord 关键词
	 */
	@RequestMapping(value = "/index")
	public String listOffer(@RequestParam Map<String, Object> param, HttpServletRequest request) throws Exception {
		log.info("获取关键词指数开始---param={}", param);
		List<Map<String, Object>> keyWordIndex = null;
		if (!param.isEmpty()) {
			keyWordIndex = keywordRevisionService.getKeyWordIndex(param);
		}
		request.setAttribute("keyWordIndex", keyWordIndex);
		request.setAttribute("keyWord", param.get("keyWord"));
		log.info("获取关键词指数结束");
		return "revisionKeyWord/keyWord_index";
	}

	/**
	 * 跳转关键词新增
	 * 
	 * @param param
	 *            param
	 * @return ModelAndView
	 * @author rtc
	 * @version 2018.07.28 12:20
	 */
	@RequestMapping(value = "/toKeyWordAdd")
	public ModelAndView toKeyWordAdd() throws Exception {
		log.info("【跳转关键词新增】---【开始】---");
		ModelAndView mv = new ModelAndView();
		List<Map<String, Object>> engine = keywordRevisionService.getEngineList();
		mv.addObject("engine", engine);
		if (Jurisdiction.roleCode("proxy")) {
			mv.addObject("proxy", "01");
		} else {
			mv.addObject("proxy", "02");
		}
		mv.setViewName("revisionKeyWord/keyWord_add");
		log.info("【跳转关键词新增】---【结束】---");
		return mv;
	}
	
	/**
	 * 关键词新增
	 * 
	 * @param param
	 *            param
	 * @return String
	 * @author rtc
	 * @version 2018.07.28 12:20
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public ReturnBody addAdmin(@RequestParam Map<String, Object> param) throws Exception {
		log.info("【用户添加关键词开始】---【开始】---");
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		try {
			if (param == null || param.isEmpty() || user == null || user.isEmpty()) {
				return rb.error("参数不能为空");
			}
			if (!param.containsKey("engineCode")) {
				return rb.error("请选择搜索引擎");
			}
			if (!param.containsKey("uploadType")) {
				return rb.error("请选择上传方式");
			}
			String uploadType = (String) param.get("uploadType");
			if("01".equals(uploadType)) {
				if (!param.containsKey("domain")) {
					return rb.error("请输入域名");
				}
				if (!param.containsKey("keyWord")) {
					return rb.error("请输入关键词");
				}
			} else {
				if (!param.containsKey("urlKeyword")) {
					return rb.error("请输入域名及关键词");
				}
			}
			param.put("currentUser", user);
			rb.run(keywordRevisionService.saveKeyWordToCustomer(param));
			log.info("【用户添加关键词开始】---【结束】---");
		} catch (Exception e) {
			log.error("添加关键词异常[{}]", e);
			return rb.error();
		}
		return rb;
	}
	
	/**
	 * 用户价格VIP等级管理列表
	 * @param params
	 *            params
	 * @param request
	 *            request
	 * @return String
	 */
	@RequestMapping(value = "/vipLevel/list")
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("用户价格VIP等级列表");
		Integer userId = Jurisdiction.getT("id", Integer.class);
		if(userId == null) {
			throw new Exception("用户未登录");
		}
		if(params == null) {
			params = new HashMap<String, Object>();
		}
		params.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(keywordRevisionService::getUserVipLevelPriceList, params);
		List<String> levelList = null;
		String clevel = configService.getConfig("vip_level");
		if(clevel != null) {
			String[] level = clevel.split(",");
			levelList = Arrays.asList(level);
		}
		request.setAttribute("levelList", levelList);
		request.setAttribute("page", page);
		return "revisionKeyWord/vip_level_list";
	}
	
	/**
	 * 启用禁用VIP等级价格
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateVipLevelState")
	@ResponseBody
	public String updateVipLevelState(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入修改方法...");
		long a = System.currentTimeMillis();
		String msg = "success";
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		try {
			if (user == null || user.isEmpty()) {
				return "error";
			}
			pam.put("update_id", user.get("id"));
			int num = keywordRevisionService.updateUserVipLevelState(pam);
			msg = num > 0 ? "success" : "error";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "error";
		}
		log.info("\r<br>执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		return msg;
	}
	
	/**
	 * 批量启用禁用VIP等级价格
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateBacthVipLevelState")
	@ResponseBody
	public String updateBacthVipLevelState(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入修改方法...");
		long a = System.currentTimeMillis();
		String msg = "success";
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		try {
			if (user == null || user.isEmpty()) {
				return "error";
			}
			pam.put("update_id", user.get("id"));
			int num = keywordRevisionService.updateBacthUserVipLevelState(pam);
			msg = num > 0 ? "success" : "error";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "error";
		}
		log.info("\r<br>执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		return msg;
	}
	
	/**
	 * 删除价格等级
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/deleteVipLevel")
	@ResponseBody
	public void deleteVipLevel(@RequestParam Map<String, Object> pam,  HttpServletRequest request) {
		log.info("进入删除方法...");
		ReturnBody rb = ReturnBody.init();
		rb = keywordRevisionService.deleteVipLevelById(pam);
		request.setAttribute("result", rb.getResult());
	}
	
	/**
	 * 添加价格等级页面
	 * @return String
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(HttpServletRequest request) {
		log.info("添加价格等级界面");
		request.setAttribute("msg", "saveVipLevel");
		return "revisionKeyWord/vip_level_add";
	}
	
	
	/**
	 * 添加价格等级
	 * @param params params
	 * @return ReturnBody
	 */
	@RequestMapping("/saveVipLevel")
	public String saveVipLevel(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存价格等级---【开始】---");
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		params.put("create_id", user.get("id"));
		ReturnBody rb = ReturnBody.init();
		try {
			rb =  keywordRevisionService.saveVipLevel(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("【保存价格等级】---【结束】---");
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	/**
	 * 修改价格等级页面
	 * @return String
	 */
	@RequestMapping(value = "/toEdit")
	public String toEdit(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("修改价格VIP等级");
		params.put("see", "01");
		List<Map<String, Object>> vipLevelPriceList = keywordRevisionService.getUserVipLevelPriceList(params);
		
		request.setAttribute("info", vipLevelPriceList.get(0));
		request.setAttribute("msg", "updateUserVipLevel");
		request.setAttribute("param", params);
		return "revisionKeyWord/vip_level_add";
	}
	
	/**
	 * 修改价格等级
	 * @param params params
	 * @return ReturnBody
	 */
	@RequestMapping("/updateUserVipLevel")
	public String updateVipLevel(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("修改价格等级---【开始】---");
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		params.put("update_id", user.get("id"));
		ReturnBody rb = ReturnBody.init();
		try {
			rb =  keywordRevisionService.updateUserVipLevel(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("【修改价格等级】---【结束】---");
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	/**
	 * 设置默认
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateVipLevelIsDefault")
	@ResponseBody
	public String updateVipLevelIsDefault(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入设置默认方法...");
		long a = System.currentTimeMillis();
		String msg = "success";
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_USER);
		try {
			if (user == null || user.isEmpty()) {
				return "error";
			}
			pam.put("update_id", user.get("id"));
			int num = keywordRevisionService.updateUserVipLevelIsDefault(pam);
			msg = num > 0 ? "success" : "error";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "error";
		}
		log.info("\r<br>执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		return msg;
	}
	
}
